package com.sk.gzwater.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;

public class HttpClientWebService {
	private Context context;
	private String url;
	private String result;
	public HttpClientWebService (Context context,String url){
		this.context = context;
		this.url = url;
	}
	
	private String callWithNoParams(){		
		try {
			HttpPost request = new HttpPost(url);
			request.addHeader("Content-Type", "application/json; charset=utf-8");
			HttpResponse httpResponse = new DefaultHttpClient().execute(request);
			if(httpResponse.getStatusLine().getStatusCode()!=404){
				result = EntityUtils.toString(httpResponse.getEntity());
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	private String callWithParams(HashMap<String, Object> paramsHash){		
		try {
			HttpPost request = new HttpPost(url);
			request.addHeader("Content-Type", "application/json; charset=utf-8");
			JSONObject jsonParams = new JSONObject();
			Iterator iterator = paramsHash.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry entry = (Map.Entry)iterator.next();
				try {
					jsonParams.put(entry.getKey().toString(),entry.getValue());
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			HttpEntity bodyEntity = new StringEntity(jsonParams.toString(),"utf8");
			request.setEntity(bodyEntity);		
			HttpResponse httpResponse = new DefaultHttpClient().execute(request);
			if(httpResponse.getStatusLine().getStatusCode()!=404){
				result = EntityUtils.toString(httpResponse.getEntity());
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
}
